package com.example.broadcast;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

public class NetworkChangedReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        //获取管理网络连接的系统服务类的实例 connectivityManager
        ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
        //NetworkInfo 对象包含网络连接的所有信息
        NetworkInfo networkInfo=connectivityManager.getActiveNetworkInfo();
//调用 NetworkInfo 的 isAvailable()方法判断是否联网
        if(networkInfo!=null&&networkInfo.isAvailable()) {
            Log.i("TAG", "网络已连接");
        }else {
            Log.i("TAG", "网络不可用");
        }
    }
}
